//引入方法
import { request } from "../../request/index.js";
let app = getApp();
const { $Toast } = require('../../iview/dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goPathUrl: app.globalData.goPathUrl,
    showTopTips: false,
    rules: [{
      name: 'username',
      rules: {required: true, message: '用户名必填'},
      },
      // {
      //   name: 'password',
      //   rules: {required: true, message: '密码必填'},
      // }
      ],
    userinfo: {}
  },
  formInputChange(e) {
    const {field} = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value
    })
  },
  submitForm() {
    this.selectComponent('#form').validate((valid, errors) => {
      console.log('valid', valid, errors)
      if (!valid) {
        const firstError = Object.keys(errors)
        if (firstError.length) {
          this.setData({
            error: errors[firstError[0]].message
          })

        }
      } else {
        wx.showToast({
          title: '校验通过'
        })
        this.addUser(this.data.formData);
      }
    })
  },
  // 注册
  addUser(data) {
    let that = this
    wx.request({
      url : this.data.goPathUrl+'/user/addUser',
      method: "POST",
      data: data,
      dataType:JSON,
      success: function (res) {
        console.log(res.data);
        wx.navigateTo({
          url: '../login/index'
        })
        wx.showToast({
          title: '注册成功！',
          icon: 'success',
          duration: 2000
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this;
    let userinfo=wx.getStorageSync("userinfo");
    that.setData({
      userinfo:userinfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
